package leetcode.code0953;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public boolean isAlienSorted(String[] words, String order) {
//    	"hlabcdefgijkmnopqrstuvwxyz"
		char[] orders = order.toCharArray();
		int[] index = new int[26];
		for (int i = 0; i < 26; i++) {
			index[orders[i] - 'a'] = i;
		}
		for (int i = 1; i < words.length; i++) {
			if (bigger(words[i - 1], words[i], index)) {
				return false;
			}
		}
		return true;
	}

	private boolean bigger(String str1, String str2, int[] order) {
		int len1 = str1.length();
		int len2 = str2.length();
		int p = 0;
		while (p < Math.min(len1, len2)) {
			int val1 = order[str1.charAt(p) - 'a'];
			int val2 = order[str2.charAt(p) - 'a'];
			if (val1 > val2) {
				return true;
			} else if (val1 < val2) {
				return false;
			}
			p++;
		}
		return len1 > len2;
	}

	@Override
	public void debug4() {
		H.compare(true, this.isAlienSorted(H.str2array2("[\"hello\",\"leetcode\"]"), "hlabcdefgijkmnopqrstuvwxyz"));
		H.compare(false, this.isAlienSorted(H.str2array2("[\"word\",\"world\",\"row\"]"), "worldabcefghijkmnpqstuvxyz"));
		H.compare(false, this.isAlienSorted(H.str2array2("[\"apple\",\"app\"]"), "abcdefghijklmnopqrstuvwxyz"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
